var gzlx='';  //故障基本信息
    var dtsydd = '' ;
    var lc = '' ;
    var cxtime = '' ;
    var wbry = '' ;
    var lxdh = '' ;
    var dtnbbm = '';
//传递故障电梯基本信息
    function getGZInfo(gzid) {
        return "gzlx=" + gzlx +
            ";cxsj=" + cxtime +
            ";wymc=" + dtsydd +
            ";dtnbbm=" + dtnbbm +
            ";gzlc=" + lc +
            ";wbry=" + wbry +
            ";lxdh=" + lxdh;
    }
localStorage.removeItem('cstit');
$(function () {
    mui.init({
        pullRefresh: {
            container: refreshContainer, //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
            up: {
                height: 30, //可选.默认50.触发上拉加载拖动距离
                auto: false, //可选,默认false.自动上拉加载一次
                contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
                contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
                callback: puful //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
            }
        }
    });
      //相册预览
     mui.previewImage();
    var ry=[];

    var record = 0;  //返回数组长度判断
    var req = {
        'req': 'getdtgz',
        'hxzid': localStorage.getItem('dthxzid'),
        'pageno': 1,
        'pagesize': Dpage
    }
    //上拉加载刷新
    function puful() {
        req.pageno = parseInt(req.pageno) + 1; //页码
        Request();
        console.log(record);
        if(record< req.pagesize ){
            this.endPullupToRefresh(true);
            
        }else{
            setTimeout(function () {
                mui('#refreshContainer').pullRefresh().enablePullupToRefresh();
                mui('#refreshContainer').pullRefresh().endPullupToRefresh();
            }, 1500)
        }
    }
    mui.ready(function () {
        //加载框
        mui.showLoading("正在加载..", "");
        Request();
        //跳转地图页面
				mui('.fault-record-list').on('tap', '.fault-loc', function () {
            var cstit = $(this).parents('li').find('.gz-tit').html(); //黑匣子故障id
            localStorage.setItem('cstit', cstit);
            //判断是否有摄像头
            var lng = localStorage.getItem('lng');
            if (lng != null && lng != '' ) {
                mui.openWindow({
                    url: 'Yearly-table-fault-map.html'
                })

            } else {
                mui.alert('提示：此故障无定位可查');
            }
        });
       
        //跳转原生界面
        mui('.fault-record-list').on('tap', 'li a', function () { 
            //$('.full-box').fadeIn();

            //判断是否有摄像头
            var verifyCode = localStorage.getItem('verifycode');
             if (verifyCode != null && verifyCode != '') {
                //播放视频条件
                var gzid = $(this).parent().attr('data-hxzgzid'); //黑匣子故障id
                $(this).attr('href', gethxzinfo(gzid));  
                window.location.href = this.href;
                //alert("要查看的视频  " + $(this).attr('href'));

            }else{
                mui.alert('提示：此故障无视频可查');
            }  

            //故障维保人员信息
            var zcdm=$(this).find('input:last-child').val();
            //var zcdm = "3110-640104-201408-0074";
            var ry = ryRequest(zcdm);
            console.log(ry);
            if (ry.length != 0 ) {
                wbry = ry[0].name;
                lxdh = ry[0].mobile;
            }else{
                wbry = '';
                lxdh = '';
            }

           //故障电梯基本信息
            gzlx = $(this).find('.fault-txt h4').html(); //故障类型
            dtsydd = $(this).find('input:first-child').val(); //故障发生地点
            dtnbbm = $(this).parent().attr('data-dtnbbm'); //故障内部编码
            //楼层 +  是否有人
            lc = $(this).find('.floor p span').html() + $(this).find('.floor h4 span').html();
            var kstime = $(this).find('.fault-im p').html(); //开始时间
            
            var thistime = getNowFormatDate(); //当前时间
            cxtime = timeCal(kstime, thistime); //持续时间

            getGZInfo(gzid); //故障基本信息方法调用
            console.log(getGZInfo(gzid));     
          
        });
        //关闭原生界面
        mui('.full-close').on('tap', 'i', function () {
            $('.full-box').fadeOut();
        })

    });

    function Request() {
        var url = gzmain + "/lift/hxz5.jsp?req=getdtgz&hxzid=" + req.hxzid + "&pageno=" + req.pageno + "&pagesize=" + req.pagesize;
        console.log("url " + url);
        $.ajax(url, {
            dataType: 'json', //服务器返回json格式数据
            type: 'post', //HTTP请求类型
            timeout: 20000, //超时时间
            async: true, //true异步加载
            success: function (data) {
                if (data.code == 2001) {
                    //返回值处理
                    var ref = data.data;
                    record = ref.length;
                    console.log(ref);
                    var html = '';
                    ref.forEach(function (v, i) {
                        var theClass = '';
                        var theInfo = '故障信息';
                        var thePeo = '无人';
                        
                        var imgUrl='http://'+localStorage.getItem('ip')+':'+localStorage.getItem('port')+'/lift/'+v.tplj;
                        //var imgUrl = 'http://123.56.47.206:80/lift' + v.tplj;
                        if (v.sfyr != 0) {
                            theClass = 'pink';
                            theInfo = '困人信息';
                            var thePeo = '有人';
                        }
                        html += `
                        <li class="monbox" data-dtnbbm='${v.dtnbbm}' data-hxzgzid='${v.hxzgzid}'>
                            <a>
                                <input type="hidden" value="${v.dtsydd}" />   
                                <h3 class="${theClass}">${theInfo}</h3>
                                <div class="mui-row">
                                    <div class="fault-im  mui-col-xs-4">
                                        <img src="${imgUrl}" data-preview-src="${imgUrl}"
                                        data-preview-group="${i}" />
                                        <p style='display:none;'>${v.gzkssj0}</p>
                                    </div>
                                    <div class="fault-txt  mui-col-xs-8">
                                        <h4 class='gz-tit'>${v.gzlx_chn}</h4>
                                        <div class="floor">
                                            <p class="mui-pull-left">楼层：<span>${v.lc}层</span></p>
                                            <h4 class="mui-pull-right"><span>${thePeo}</span></h4>
                                        </div>
                                    </div>
                                </div>
                                <div class="nbbm fault-txt">
                                    <p>内部编码：<span>${v.dtnbbm}</span></p>
                                    <p>上报时间：${v.gzkssj0} </p>
                                </div>
                                <input type="hidden" value="${v.dtzcdm}" /> 
                            </a>
                            <div class="fault-loc">
                                <span></span>
                            </div>
                        </li>
                        `
                    });
                    $('.fault-record-list').append(html);

                    //隐藏加载框
                    mui.hideLoading(function () { //隐藏后的回调函数  
                    });
                } else if (data.code == 2002) {
                    mui.alert(data.msg,function(){
                        mui.openWindow({
                            url: '../../login.html'
                        })
                    });
                } else {
                    console.log(data);
                    mui.alert(data.msg,function(){
                        mui.openWindow({
                            url: '../../404.html'
                        })
                    });
                }
            },
            error: function (xhr, type, errorThrown) { //异常处理； console.log(type);
                alert('登录异常，请您重新登录');
                mui.openWindow({
                    url: '../../login.html',
                    id: 'login.html'
                })
            }
        });
    }
    
    // 视频播放需要数据(黑匣子内信息 包含坐标)
    function gethxzinfo(gzid) {
        var at = localStorage.getItem('accesstoken');
        var deviceSerial = localStorage.getItem('deviceserial');
        var verifyCode = localStorage.getItem('verifycode');
        var appkey = localStorage.getItem('appkey');
         //隐藏加载框
         mui.hideLoading(function () { //隐藏后的回调函数  
         });
        return "dt:req=gzvideo;at=" + at + ";deviceSerial=" + deviceSerial + ";verifyCode=" + verifyCode + ";gzid=" + gzid +";appkey="+appkey ;
    }
    //时间计算
    function timeCal(time1,time2) {
        var date1 = new Date(time1.replace(/-/g, "/"));
        var date2 = new Date(time2.replace(/-/g, "/"));
        var s1 = date1.getTime(),
            s2 = date2.getTime();
        var total = (s2 - s1) / 1000;
        var day = parseInt(total / (24 * 60 * 60)); //计算整数天数
        var afterDay = total - day * 24 * 60 * 60; //取得算出天数后剩余的秒数
        var hour = parseInt(afterDay / (60 * 60)); //计算整数小时数
        var afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60; //取得算出小时数后剩余的秒数
        var min = parseInt(afterHour / 60); //计算整数分
        var afterMin = parseInt(total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); //取得算出分后剩余的秒数
        return day+"天"+hour+"时"+min+"分"+afterMin+"秒";
    }

    //请求维保人员信息
    function ryRequest(zcdm) {
        var url = domain + "/dtwbgl/dtdesc.h5";
        var ryreq={
            'dtcode': zcdm,
            'userid': localStorage.getItem('userid'),
            'token': localStorage.getItem('token')
        }
        
        $.ajax(url, {
            data: ryreq,
            dataType: 'json', //服务器返回json格式数据
            type: 'post', //HTTP请求类型
            timeout: 30000, //超时时间
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            },
            async: false, //true异步加载
            success: function (data) {
                if (data.code == 2001) {
                    //console.log(data);
                    // 维保人员
                    var ref=data.data;
                    if(JSON.stringify(ref) != "{}"){
                        ry = ref.ry;
                    }else{
                        ry=[];
                    }     
                } else if (data.code == 2002) {
                    mui.alert(data.msg,function(){
                        mui.openWindow({
                            url: '../../login.html'
                        })
                    });
                    console.log(data);
                } else {
                    mui.alert(data.msg,function(){
                        mui.openWindow({
                            url: '../../404.html'
                        })
                    });
                }
            },
            error: function (xhr, type, errorThrown) { //异常处理； console.log(type);
                alert('登录异常，请您重新登录');
                mui.openWindow({
                    url: '../../login.html',
                    id: 'login.html'
                })
                console.log(data)
            }
        });
        return ry;
    }

    //排序
    function compare(property) {
        return function (a, b) {
            var value1 = a[property];
            var value2 = b[property];
            return value1 - value2;
        }
    }

})
